package com.yenlien.traceyun.control.controller;

import com.yenlien.traceyun.control.entity.BankInfo;
import com.yenlien.traceyun.control.entity.LoanInfo;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.control.service.BankInfoService;
import com.yenlien.traceyun.control.service.LoanInfoService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import javafx.application.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * 贷款申请
 */
@RestController
@RequestMapping("/loanInfo")
public class LoanInfoController extends BaseController {

    @Autowired
    private LoanInfoService loanInfoService;

    @Autowired
    private BankInfoService bankInfoSdervice;

    /**
     * 列表分页查询3
     */
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody LoanInfo loanInfo) {
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        BankInfo byBankId = bankInfoSdervice.findByBankId(companyId);
        if (byBankId != null) {
            loanInfo.setBankInfoId(byBankId.getId());
        } else {
            loanInfo.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return buildPage(loanInfoService.findByList(loanInfo));
    }

    /**
     * 获取银行层级
     */
    @RequestMapping(value = "/bankInfo", method = RequestMethod.GET)
    public Object bankInfo() {
        return loanInfoService.bankInfo();
    }

    /**
     * 申请贷款
     */
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public Object apply(@RequestBody LoanInfo loanInfo) {
        loanInfo.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return loanInfoService.applyInfo(loanInfo);
    }

    /**
     * 根据ID查询单个详情
     * Request Method: POST
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return loanInfoService.findById(id);
    }

    /**
     * 申请贷款 审核，银行端, 通过后，调用生成还款计划表的（要传申请的ID 赋值给loan_id）
     */
    @RequestMapping(value = "/applyStatus", method = RequestMethod.POST)
    public Object applyStatus(@RequestBody LoanInfo loanInfo) {
        Object number = loanInfoService.updateStatus(loanInfo);
        if (number.equals(0)) {
            return fail("操作失败");
        }
        return success("操作成功");
    }

    /**
     * 银行端：   我的客户（列表查询）
     */
    @RequestMapping(value = "/findByCustomer", method = RequestMethod.POST)
    public Object findByCustomer(@RequestBody LoanInfo loanInfo) {
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        BankInfo byBankId = bankInfoSdervice.findByBankId(companyId);
        if (byBankId != null) {
            loanInfo.setBankInfoId(byBankId.getId());
        }
        return buildPage(loanInfoService.findByCustomer(loanInfo));
    }

    /**
     * 银行端    我的客户根据ID  查询单个
     */
    @RequestMapping(value = "/findByIdCustomer/{id}", method = RequestMethod.GET)
    public Object findByIdCustomer(@PathVariable("id") String id) {
        return loanInfoService.findByIdCustomer(id);
    }

    /**
     * 合作社 or 银行    贷款管理列表查询
     */
    @RequestMapping(value = "/findListRepay", method = RequestMethod.POST)
    public Object findByIdrepayment(@RequestBody LoanInfo loanInfo) {
        Page<LoanInfo> byList = loanInfoService.findByIdrepayment(loanInfo);
        return buildPage(byList);
    }

    /**
     * 合作社 or 银行    贷款管理根据ID查询
     */
    @RequestMapping(value = "/findIdRepay/{id}", method = RequestMethod.GET)
    public Object findIdRepay(@PathVariable("id") String id) {
        return loanInfoService.findIdRepay(id);
    }


}
